package com.deryun.tyl.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.deryun.tyl.bean.MutiPic;
import com.deryun.tyl.service.impl.MutiPicServiceImpl;

@Controller
@RequestMapping("/mutiPic")
public class MutiPicController {

	@Resource(name="mutiPicServiceImpl")
	private MutiPicServiceImpl mutiPicServiceImpl;
	
	@RequestMapping("/getPicByType")
	public List<MutiPic> getPicByType(int type){
		return mutiPicServiceImpl.getPicByType(type);
	}
	
	@RequestMapping("/addPic")
	@ResponseBody
	public String addPic(MutiPic mutiPic,@RequestParam("file") MultipartFile file){
		if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(mutiPic.picPath + "-uploaded")));
                stream.write(bytes);
                stream.close();
                return "You successfully uploaded " + mutiPic.picPath + " into " + mutiPic.picPath + "-uploaded !";
            } catch (Exception e) {
                return "You failed to upload " + mutiPic.picPath + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + mutiPic.picPath + " because the file was empty.";
        }
		
		
	}
	
	@RequestMapping("/delPic")
	public int delPic(int id){
		return mutiPicServiceImpl.delPic(id);
	}
	
	@RequestMapping("/uploadPic")
	public void uploadPic(MultipartFile file){
		
	}
}
